﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BoAddonBiz_FreightForwarder.BusinessBase;
using SAPbouiCOM;
using System.Text.RegularExpressions;
using SAPbobsCOM;

namespace BoAddonBiz_FreightForwarder.Air
{
    [ControlB1Form(IsNew = true, FormType = B1FormTypes.ado_AVA_FF_BusinessOperation)]
    class B1Form_BusinessOperation_Air : B1Form_BusinessOperation_Base
    {
        #region override
        protected override string AVA_OBNO { get { return "@AVA_FF_OBNO"; } }
        protected override string AVA_BNO1 { get { return "@AVA_FF_BNO1"; } }
        protected override string AVA_BNO2 { get { return "@AVA_FF_BNO2"; } }
        protected override string AVA_BNO3 { get { return "@AVA_FF_BNO3"; } }
        protected override string AVA_BNO4 { get { return "@AVA_FF_BNO4"; } }
        protected override string AVA_BNO5 { get { return "@AVA_FF_BNO5"; } }
        protected override string AVA_ODLG { get { return "@AVA_FF_ODLG"; } }
        protected override string AVA_DLG1 { get { return "@AVA_FF_DLG1"; } }
        protected override string AVA_DLG2 { get { return "@AVA_FF_DLG2"; } }
        protected override string AVA_DLG3 { get { return "@AVA_FF_DLG3"; } }
        protected override string AVA_DLG6 { get { return "@AVA_FF_DLG6"; } }
        protected override string AVA_AirOrder { get { return "AVA_FF_AirOrder"; } }
        protected override string AVA_AirOrderUsed { get { return "AVA_FF_AirOrderUsed"; } }
        protected override string AVA_OAOU { get { return "@AVA_FF_OAOU"; } }
        protected override string AVA_OAOD { get { return "@AVA_FF_OAOD"; } }
        protected override string SourceObject { get { return "AVA_FF_Delegate"; } }
        protected override string BusinessType { get { return "FF"; } }
        protected override string[] SumMapping
        {
            get { return new string[] { "U_Quantity:U_Quantity", "U_GrossWeight:U_GrossWeight", "U_BillingWeight:U_BillingWeight", "U_Volume:U_Volume", "U_CargoValue:U_CargoValue" }; }
        }
        protected override string[] CB_LABEL_UDS_DBDS_OBJECT_TABLE_DESC
        {
            get
            {
                return new string[] {
            "cb_opup:btn_opup:OPUP_UID:U_IsPickup:AVA_FF_Pickup:@AVA_FF_OPUP:提货",
            "cb_oisu:btn_oisu:OISU_UID:U_IsInsure:AVA_FF_Insure:@AVA_FF_OISU:投保",
            "cb_oatc:btn_oact:OATC_UID:U_IsCustoms:AVA_FF_ApplyCustoms:@AVA_FF_OATC/@AVA_FF_ATC1:出口报关/报检",
            "cb_ostg:btn_ostg:OSTG_UID:U_IsWarehouse:AVA_FF_Storage:@AVA_FF_OSTG/@AVA_FF_STG1:仓储",
            "cb_odlv:btn_odlv:ODLV_UID:U_IsDelivery:AVA_FF_Deliver:@AVA_FF_ODLV:送货",
            "cb_octc:btn_octc:OCTC_UID:U_IsClearance:AVA_FF_Clearance:@AVA_FF_OCTC/@AVA_FF_CTC1:进口报关/报检",
            "cb_oarp:btn_oarp:OARP_UID:U_IsArrivePort:AVA_FF_ArrivePort:@AVA_FF_OARP:到港"
            };
            }
        }
        #endregion
        #region constractor
        public B1Form_BusinessOperation_Air()
            : base()
        {
            this.frm_ItemEvent += new SAPBoAddon.IB1FormEvents.B1ItemEventEventHandler(B1Form_BusinessOperation_Air_frm_ItemEvent);
            this.frm_FormDataEvent += new SAPBoAddon.IB1FormEvents.B1FormDataEventEventHandler(B1Form_BusinessOperation_Air_frm_FormDataEvent);
            this.frm_MenuEvent += new SAPBoAddon.IB1FormEvents.B1MenuEventEventHandler(B1Form_BusinessOperation_Air_frm_MenuEvent);
            this.frm_ChooseFormListEvent += new SAPBoAddon.IB1FormEvents.B1ChooseFormListEventEventHandler(B1Form_BusinessOperation_Air_frm_ChooseFormListEvent);
        }
        #endregion
        #region events
        void B1Form_BusinessOperation_Air_frm_ChooseFormListEvent(SAPBoAddon.B1AddonBase.B1ChooseFormList pVal, ref bool BubbleEvent)
        {
            if (pVal.Before_Action)
            {
                SAPbouiCOM.ChooseFromList cfl = this.CurrentForm.ChooseFromLists.Item(pVal.ChooseFromListUID);
                SAPbouiCOM.Conditions cons;
                SAPbouiCOM.Condition con;
                switch (pVal.ChooseFromListUID)
                {
                    case "CFL_OCRD_D"://指定代理
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "QryGroup5";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_OR;
                        con = cons.Add();
                        con.Alias = "QryGroup6";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_OR;
                        con = cons.Add();
                        con.Alias = "QryGroup7";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_AND;
                        con = cons.Add();
                        con.Alias = "validFor";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                    case "CFL_OCRD_ED"://目的港代理
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "QryGroup7";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_AND;
                        con = cons.Add();
                        con.Alias = "validFor";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                    case "CFL_OCRD_TP"://中转港代理
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "QryGroup7";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_AND;
                        con = cons.Add();
                        con.Alias = "validFor";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                    case "CFL_OCRD_T":
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "QryGroup7";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_AND;
                        con = cons.Add();
                        con.Alias = "validFor";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                    case "CFL_OCRD_F"://承运人
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "QryGroup5";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_OR;
                        con = cons.Add();
                        con.Alias = "QryGroup6";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_OR;
                        con = cons.Add();
                        con.Alias = "QryGroup7";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_AND;
                        con = cons.Add();
                        con.Alias = "validFor";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                }
            }
            else
            {
                //added by WangPeng 2015年3月15日 23:37:35
                if (pVal.ChooseFromListUID == "CFL_Vendor" || pVal.ChooseFromListUID == "CFL_OrgID")//分单收货人
                {
                    SAPbouiCOM.DBDataSource dbds4 = this.CurrentForm.DataSources.DBDataSources.Item(AVA_BNO4);
                    dbds4.SetValue("U_Sender", 0, pVal.SelectedObjects.GetValue("U_SupplierName", 0).ToString());
                    dbds4.SetValue("U_VendorID", 0, pVal.SelectedObjects.GetValue("U_VendorID", 0).ToString());
                    dbds4.SetValue("U_OrgID", 0, pVal.SelectedObjects.GetValue("U_OrgID", 0).ToString());
                }
                if (pVal.ChooseFromListUID == "CFL_OCRD_S2")//分单发货人
                {
                    SAPbouiCOM.DBDataSource dbds4 = this.CurrentForm.DataSources.DBDataSources.Item(AVA_BNO4);
                    String CardCode = pVal.SelectedObjects.GetValue("CardCode", 0).ToString().Trim();
                    SAPbobsCOM.Recordset oRst = SBOCompany.GetBusinessObject(BoObjectTypes.BoRecordset);
                    string SQL_GetVendorId = string.Format("SELECT LicTradNum,U_Address,Address FROM [CRD1] WHERE AdresType='S' AND CardCode=N'{0}' ", CardCode);
                    oRst.DoQuery(SQL_GetVendorId);
                    if (oRst.RecordCount > 0)
                    {
                        oRst.MoveFirst();
                        dbds4.SetValue("U_SendDetail", 0, oRst.Fields.Item("U_Address").Value);// pVal.SelectedObjects.GetValue("U_Address", 0).ToString()
                        dbds4.SetValue("U_VendorID", 0, pVal.SelectedObjects.GetValue("U_ShortName", 0).ToString());//pVal.SelectedObjects.GetValue("LicTradNum", 0).ToString()
                        dbds4.SetValue("U_OrgID", 0, pVal.SelectedObjects.GetValue("LicTradNum", 0).ToString());
                    }

                }

            }

        }

        void B1Form_BusinessOperation_Air_frm_MenuEvent(SAPBoAddon.B1AddonBase.B1MenuEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            {

            }
            else
            {
                if (pVal.MenuUID == "1287")
                {
                    SBLOtherPayChange(this.CurrentForm, this.CurrentForm.DataSources.DBDataSources.Item(AVA_BNO4).GetValue("U_OtherPayMethod", this.CurrentForm.DataSources.DBDataSources.Item(AVA_BNO4).Offset).Trim());//2012-05-30 12:08:52 by Webb.Wen
                    this.CurrentForm.DataSources.DBDataSources.Item(AVA_OBNO).SetValue("U_IsOnwardTransit", 0, "");//续程过境中转
                    this.CurrentForm.DataSources.DBDataSources.Item(AVA_OBNO).SetValue("U_OnwardTransMode", 0, "");//续程过境中转
                    this.CurrentForm.DataSources.DBDataSources.Item(AVA_OBNO).SetValue("U_TargetMBLNum", 0, "");//续程过境中转
                    this.CurrentForm.DataSources.DBDataSources.Item(AVA_OBNO).SetValue("U_TargetSBLNum", 0, "");//续程过境中转
                    this.CurrentForm.DataSources.DBDataSources.Item(AVA_OBNO).SetValue("U_TargetSerial", 0, "");//续程过境中转
                    this.CurrentForm.DataSources.DBDataSources.Item(AVA_OBNO).SetValue("U_TargetEntry", 0, "");//续程过境中转
                    #region 过境部分
                    SetTransit();
                    #endregion
                }
            }
        }

        void B1Form_BusinessOperation_Air_frm_ItemEvent(SAPBoAddon.B1AddonBase.B1ItemEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.Before_Action)
            {
                #region 主单号录入格式限制
                //if (pVal.ItemUID == "1000013" && pVal.EventType == BoEventTypes.et_VALIDATE)
                //{
                //    String MBLNumber = SAPBoAddon.B1Assistant.Item.GetValue(CurrentForm, pVal.ItemUID).ToString();
                //    if (string.IsNullOrEmpty(MBLNumber)) return;
                //    //if (MBLNumber.Substring(3, 1) != "-")
                //    if (!new Regex(@"\w{3}\w{8}").Match(MBLNumber).Success)
                //    {
                //        SBOApp.MessageBox("主单号的格式为：XXXXXXXXXXX\r\n请检查录入的是否正确？");
                //        BubbleEvent = false;
                //    }
                //}

                #endregion
            }
            else
            {
                #region pane
                if (pVal.ItemUID == "tab_0" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    CurrentForm.PaneLevel = 1;
                }
                if (pVal.ItemUID == "tab_1" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    CurrentForm.PaneLevel = 2;
                }
                if (pVal.ItemUID == "tab_2" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    CurrentForm.PaneLevel = 3;
                }
                if (pVal.ItemUID == "tab_3" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    CurrentForm.PaneLevel = 4;
                }
                if (pVal.ItemUID == "tab_4" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    CurrentForm.PaneLevel = 5;
                }
                #endregion
                #region  毛重、体积 变化后重新计算计费重
                if ((pVal.ItemUID == "cargo_8"/*毛重*/ || pVal.ItemUID == "cargo_12"/*体积*/ ) && pVal.EventType == BoEventTypes.et_VALIDATE)
                {
                    DBDataSource dbds1 = this.CurrentForm.DataSources.DBDataSources.Item(AVA_BNO1);
                    string str_GrossWeight = SAPBoAddon.B1Assistant.Item.GetValue(this.CurrentForm, pVal.ItemUID).ToString().Trim();
                    decimal GrossWeight = string.IsNullOrEmpty(str_GrossWeight) ? 0 : Convert.ToDecimal(str_GrossWeight);
                    decimal Volume = Convert.ToDecimal(dbds1.GetValue("U_Volume", dbds1.Offset).Trim());
                    decimal BillWeight = GrossWeight >= Volume * 167 ? GrossWeight : Volume * 167; //毛重对比 体积*167 取两者中较大者（根据闫露露提出修改） added By WangPeng 2012年8月14日 11:57:51
                    dbds1.SetValue("U_BillingWeight", dbds1.Offset, BillWeight.ToString());
                    Utils.MaterialSum(this.CurrentForm, AVA_OBNO, AVA_BNO1);
                }
                #endregion
                #region
                /*这的事件一定不可以用 et_CLICK 代替，否则你将看到相反的效果
                   设置分单页签是否显示
                 */
                if (pVal.ItemUID == "914_1" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    this.SetTabHidden(pVal.ItemUID, "tab_4");
                }
                #endregion
                #region 续程
                //业务单
                if (pVal.ItemUID == "linkbtn_B" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    Utils.OpenUserDeFinedObjectForm(this.CurrentForm, pVal.ItemUID, "AVA_FF_BusinessOpera");
                }
                if (pVal.ItemUID == "x-59" && (pVal.EventType == BoEventTypes.et_ITEM_PRESSED || pVal.ItemChanged))
                {
                    if (SAPBoAddon.B1Assistant.Item.GetValue(CurrentForm, "x-59").ToString().Trim() == "Y")
                    {
                        SetItemVisible(true);
                    }
                    else
                    {
                        SetItemVisible(false);
                    }
                }
                //续程
                if (pVal.ItemUID == "x-GoOn" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    if (CurrentForm.Mode == BoFormMode.fm_OK_MODE)
                    {
                        string transType = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OBNO).GetValue("U_OnwardTransMode", 0).Trim();
                        if (string.IsNullOrEmpty(transType))
                        {
                            SBOApp.MessageBox("请选择业务类型，再进行操作！");
                            return;
                        }
                        if (!string.IsNullOrEmpty(this.CurrentForm.DataSources.DBDataSources.Item(AVA_OBNO).GetValue("U_TargetEntry", 0).Trim()))
                        {
                            SBOApp.MessageBox("已生成业务单，不需要再生成！");
                            return;
                        }
                        Recordset rs = this.SBOCompany.GetBusinessObject(BoObjectTypes.BoRecordset);
                        rs.DoQuery(String.Format("select ISNULL(U_Type,N'Air') AS U_Type  from [@AVA_FF_OMOT] where Code='{0}'", transType));
                        if (rs.RecordCount == 1)
                        {
                            string onward = Convert.ToString(rs.Fields.Item("U_Type").Value);
                            switch (onward)
                            {
                                case "Air":
                                    this.ShowForm("AVA_FF_BusinessOpera", this.CurrentForm.UniqueID);
                                    break;
                                case "Sea":
                                    this.ShowForm("AVA_SE_BusinessOpera", this.CurrentForm.UniqueID);
                                    break;
                                case "Land":
                                    this.ShowForm("AVA_LN_BusinessOpera", this.CurrentForm.UniqueID);
                                    break;
                            }
                        }
                    }
                    else
                    {
                        SBOApp.MessageBox("请保存数据后，再进行操作！");
                    }
                }
                #endregion
                #region 主单计费重发生变化时
                if (pVal.ItemUID == "ex8" && pVal.ItemChanged)
                {
                    string BillingWeight = CurrentForm.DataSources.DBDataSources.Item(AVA_BNO3).GetValue("U_BillingWeight", 0).Trim();
                    CurrentForm.DataSources.DBDataSources.Item(AVA_BNO3).SetValue("U_BillingWeights", 0, BillingWeight);
                }
                #endregion
                #region 小写转换成大写
                if (pVal.ItemChanged && new string[] { "1000015", "1000017", "1000019", "cargo_4", "cargo_48", "171", "173", "175", "1000083", "1000081", "1000079" }.Contains<string>(pVal.ItemUID))
                {
                    SAPBoAddon.B1Assistant.Item.SetValue(CurrentForm, pVal.ItemUID, SAPBoAddon.B1Assistant.Item.GetValue(CurrentForm, pVal.ItemUID).ToString().ToUpper());
                }
                #endregion
            }
        }

        void B1Form_BusinessOperation_Air_frm_FormDataEvent(SAPBoAddon.B1AddonBase.B1BusinessObjectInfo pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            {

            }
            else
            {
                #region 主单领用
                if (pVal.EventType == BoEventTypes.et_FORM_DATA_ADD && pVal.ActionSuccess)//主对象保存成功之后
                {
                    //主单领用
                    //AddAirOrderUsed(); by WangPeng 2012年9月20日 10:14:36
                }
                #endregion
                if (pVal.EventType == BoEventTypes.et_FORM_DATA_LOAD && pVal.ActionSuccess)
                {
                    SBLOtherPayChange(this.CurrentForm, this.CurrentForm.DataSources.DBDataSources.Item(AVA_BNO4).GetValue("U_OtherPayMethod", this.CurrentForm.DataSources.DBDataSources.Item(AVA_BNO4).Offset).Trim());//Vincent.yin
                    MBLOtherPayChange(this.CurrentForm, this.CurrentForm.DataSources.DBDataSources.Item(AVA_BNO3).GetValue("U_OtherPayMethod", this.CurrentForm.DataSources.DBDataSources.Item(AVA_BNO3).Offset).Trim());//Vincent.yin
                    //设置分单页签是否显示
                    this.SetTabHidden("914_1", "tab_4");
                    #region 过境部分
                    SetTransit();
                    #endregion
                }
                #region 更新仓储
                if ((pVal.EventType == BoEventTypes.et_FORM_DATA_ADD || pVal.EventType == BoEventTypes.et_FORM_DATA_UPDATE))//主对象保存成功之后
                {
                    //更新仓库单据中的“见款放单”、“报关方式”、“结算方式”                    
                    UpdateWarehouseData(ref BubbleEvent);
                    this.UpdateSEDLData(); //更新SEDL字段
                    this.Update_OCTCClearanceDate();//更新进口报关单"滞报日期"
                }
                #endregion
            }
        }
        #endregion
        #region protected override
        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {
            FormSetting.FormType = B1FormTypes.ado_AVA_FF_BusinessOperation;
            FormSetting.FormFileName = "AVA_FF_BusinessOperaForm.srf";
            #region 窗体控件控制
            string[] NewModeItms = new string[] { "274", "275", "277", "278", "279", "276", "268", "269", "270", "271", "272", "273" };
            #region 添加状态
            FormSetting.FormModeSetting.itmNewModeEnable = Utils.GetFormItems(FormSetting.FormXmlDocument, true, NewModeItms);
            FormSetting.FormModeSetting.itmNewModeDisable = NewModeItms;
            #endregion
            #endregion
            base.FormInfoSetting(FormSetting);
        }
        protected override void FormEditModeChange(SAPBoAddon.frmItemEditMode frmEditMode)
        {
            base.FormEditModeChange(frmEditMode);
            switch (frmEditMode)
            {
                case SAPBoAddon.frmItemEditMode.fe_AddMode:
                    this.CurrentForm.DataSources.DBDataSources.Item(AVA_BNO3).SetValue("U_NCV", 0, "N.C.V");
                    this.CurrentForm.DataSources.DBDataSources.Item(AVA_BNO3).SetValue("U_NVD", 0, "N.V.D");
                    this.CurrentForm.DataSources.DBDataSources.Item(AVA_BNO4).SetValue("U_NCV", 0, "N.C.V");
                    this.CurrentForm.DataSources.DBDataSources.Item(AVA_BNO4).SetValue("U_NVD", 0, "N.V.D");
                    break;

            }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <author>WangPeng</author>
        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {
            base.InitializeForm(InitPar);
            Utils.InitialItemComboBox(this.SBOCompany, this.CurrentForm.Items.Item("cargo_72"), "Code", "Name", "@AVA_FF_BAMD", "Code", null, null, true);//成交方式 by WangPeng 2012年9月14日 13:17:19
            Utils.InitialItemComboBox(this.SBOCompany, this.CurrentForm.Items.Item("x-57"), "Code", "Name", "@AVA_FF_OMOT", "Code", null, "U_Type='Air' or U_Type IS NULL");//运输方式
            Utils.InitialItemComboBox(this.SBOCompany, this.CurrentForm.Items.Item("ex3"), "Code", "Name", "@AVA_FF_OPRI", "Code", null, "U_Type = 'Number'");
            Utils.InitialItemComboBox(this.SBOCompany, this.CurrentForm.Items.Item("ex6"), "Code", "Name", "@AVA_FF_OPRI", "Code", null, "U_Type = 'Weight'");
            Utils.InitialItemComboBox(this.SBOCompany, this.CurrentForm.Items.Item("ex9"), "Code", "Name", "@AVA_FF_OPRI", "Code", null, "U_Type = 'Weight'");
            Utils.InitialItemComboBox(this.SBOCompany, this.CurrentForm.Items.Item("ex12"), "Code", "Name", "@AVA_FF_OPRI", "Code", null, "U_Type = 'Volume'");
            #region 过境部分
            SetTransit();
            #endregion
        }
        #endregion
        #region methods
        #region 续程
        /// <summary>
        /// 过境
        /// </summary>
        void SetTransit()
        {
            SAPbouiCOM.Form oForm;
            SAPbouiCOM.DBDataSource dbds;
            string IsTransit = string.Empty;
            try
            {
                oForm = CurrentForm;
                dbds = oForm.DataSources.DBDataSources.Item(AVA_OBNO);

                IsTransit = dbds.GetValue("U_IsOnwardTransit", dbds.Offset).Trim();
                if (IsTransit == "Y")
                {
                    SetItemVisible(true);
                }
                else
                {
                    SetItemVisible(false);
                    if (!string.IsNullOrEmpty(dbds.GetValue("U_TargetEntry", dbds.Offset).Trim()))
                    {
                        SAPBoAddon.B1Assistant.Item.SetEnabled(oForm, "x-GoOn", false);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 设置过境字段是否可用
        /// </summary>
        /// <param name="oForm"></param>
        /// <param name="Value"></param>
        void SetItemVisible(bool Value)
        {
            SAPbouiCOM.Form oForm;
            try
            {
                oForm = CurrentForm;
                string[] item = { "x-rect", "x-57", "x-58", "x-10", "x-11", "x-12", "x-13", "x-15", "x-16", "x-17", "x-GoOn" };
                SAPBoAddon.B1Assistant.Item.SetVisible(CurrentForm, item, Value);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #region 更新仓储
        /// <summary>
        /// 更新仓储的字段
        /// </summary>
        private void UpdateWarehouseData(ref bool BubbleEvent)
        {
            string DocEntry = string.Empty;
            string SerialNum = string.Empty;
            string IsWarehouse = string.Empty;
            string SQL = string.Empty;
            bool IsOrderExists = false;
            try
            {
                DocEntry = CurrentForm.DataSources.DBDataSources.Item(AVA_OBNO).GetValue("DocEntry", 0).Trim();
                SerialNum = CurrentForm.DataSources.DBDataSources.Item(AVA_OBNO).GetValue("U_SerialNum", 0).Trim();
                IsWarehouse = CurrentForm.DataSources.DBDataSources.Item(AVA_OBNO).GetValue("U_IsWarehouse", 0).Trim();
                Recordset rs = this.SBOCompany.GetBusinessObject(BoObjectTypes.BoRecordset);
                SBOCompany.StartTransaction();
                SQL = "SELECT COUNT('A') FROM [@AVA_FF_OSTG] Where U_BaseEntry = '{0}'";

                rs.DoQuery(string.Format(SQL, DocEntry));
                if (rs.Fields.Item(0).Value > 0)
                {
                    IsOrderExists = true;
                }
                if (IsWarehouse == "Y")
                {
                    if (IsOrderExists)
                    {
                        //更新仓库信息
                        SQL = @"
                            UPDATE T1
	                            SET
	                            T1.U_Operator = T0.U_Operator
	                            ,T1.U_DocDate = T0.U_DocDate
	                            ,T1.U_BaseEntry = T0.DocEntry
	                            ,T1.U_BaseSerial = T0.U_SerialNum
	                            ,T1.U_CardCode = T0.U_CardCode
	                            ,T1.U_CardName = T0.U_CardName
	                            ,T1.U_CntctCode = T0.U_CntctCode
	                            ,T1.U_MBLNumber = T0.U_MBLNumber
	                            ,T1.U_SBLNumber = T0.U_SBLNumber
	                            ,T1.U_TransportType = T0.U_TransportType
	                            ,T1.U_ReleaseAfterPay = T0.U_ReleaseAfterPay
	                            ,T1.U_Customs = T0.U_Customs
	                            ,T1.U_ClearingForm = T0.U_ClearingForm
	                            ,T1.UpdateDate = CONVERT(char(10),getdate(),120)
	                            ,T1.UpdateTime = cast(datepart(hh,getdate()) as int)*100 + cast(datepart(mi,getdate()) as int)
                            FROM [@AVA_FF_OBNO] T0
	                            INNER JOIN [@AVA_FF_OSTG] T1 ON T0.DocEntry = T1.U_BaseEntry
                            Where T0.DocEntry = '{0}'
                            UPDATE T3 SET
	                            T3.U_CargoType = T1.U_CargoType
	                            ,T3.U_CargoName = T1.U_CargoName
	                            ,T3.U_ShipMark = T1.U_ShoppingMark
	                            ,T3.U_CargoSize = T1.U_CargoSize
	                            ,T3.U_Quantity = T1.U_Quantity
                                ,T3.U_GrossWeight = T1.U_GrossWeight
	                            ,T3.U_Weight = T1.U_BillingWeight
	                            ,T3.U_Volume = T1.U_Volume
                                ,T3.U_Flight = T0.U_Flight
								,T3.U_FreightPayMethod = T4.U_FreightPayMethod
								,T3.U_senderCode = T4.U_senderCode
								,T3.U_Sender = T4.U_Sender
								,T3.U_ReceiverCode = T4.U_ReceiverCode
								,T3.U_Receiver = T4.U_Receiver
                                ,T3.U_StartPortCode = T0.U_StartPortCode
                                ,T3.U_StartPortThCode = T0.U_StartPortThCode
                                ,T3.U_StartPort = T0.U_StartPort
                                ,T3.U_StartPortTwCode = T0.U_StartPortTwCode
                                ,T3.U_StartPortENamel = T0.U_StartPortENamel
                                ,T3.U_TrsferPortCode = T0.U_TrsferPortCode
                                ,T3.U_TrsferPortThCode = T0.U_TrsferPortThCode
                                ,T3.U_TrsferPort = T0.U_TrsferPort
                                ,T3.U_TrsferPortTwCode = T0.U_TrsferPortTwCode
                                ,T3.U_TrsferPortENamel = T0.U_TrsferPortENamel
                            FROM [@AVA_FF_OBNO] T0
	                            INNER JOIN [@AVA_FF_BNO1] T1 ON T0.DocEntry = T1.DocEntry
	                            INNER JOIN [@AVA_FF_OSTG] T2 ON T0.DocEntry = T2.U_BaseEntry
	                            INNER JOIN [@AVA_FF_STG1] T3 ON T2.DocEntry = T3.DocEntry
	                            INNER JOIN [@AVA_FF_BNO4] T4 ON T0.DocEntry = T4.DocEntry
                            Where T0.DocEntry = '{0}'
                           ";
                        rs.DoQuery(string.Format(SQL, DocEntry));
                    }
                    else
                    {
                        //添加仓库单据
                        SQL = @"
                            Declare @Autokey int
                            SELECT @Autokey = AutoKey FROM ONNM Where ObjectCode = 'AVA_FF_Storage'
                            INSERT INTO [@AVA_FF_OSTG] (DocEntry,DocNum,Period,Instance,Series,Canceled,Object,UserSign,Status,CreateDate,CreateTime,DataSource
	                            ,U_Operator,U_DocDate,U_SerialNum,U_BaseEntry,U_BaseSerial,U_Status,U_CardCode,U_CardName,U_CntctCode,U_MBLNumber,U_SBLNumber,U_TransportType
                                ,U_ReleaseAfterPay,U_Customs,U_ClearingForm) 
	                            Values (@Autokey,@Autokey,'29',0,54,'N','AVA_FF_Storage',{0},'O',CONVERT(char(10),getdate(),120),cast(datepart(hh,getdate()) as int)*100 + cast(datepart(mi,getdate()) as int),'I'
	                            ,'{1}',CONVERT(char(10),getdate(),120),'','{2}','{3}','P','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}')
                            INSERT INTO [@AVA_FF_STG1] (DocEntry,LineId,VisOrder,Object,U_CargoType,U_CargoName,U_ShipMark,U_CargoSize,U_Quantity,U_GrossWeight,U_Weight,U_Volume
                                                        ,U_Flight
                                                        ,U_StartPortCode,U_StartPortThCode,U_StartPort,U_StartPortTwCode,U_StartPortENamel,U_TrsferPortCode,U_TrsferPortThCode,U_TrsferPort,U_TrsferPortTwCode,U_TrsferPortENamel
                                                        ,U_FreightPayMethod,U_senderCode,U_Sender,U_ReceiverCode,U_Receiver)
                                SELECT @AutoKey,1,1,'AVA_FF_Storage',T1.U_CargoType,T1.U_CargoName,T1.U_ShoppingMark,T1.U_CargoSize,T1.U_Quantity,T1.U_GrossWeight,T1.U_BillingWeight,T1.U_Volume
                                    ,T0.U_Flight
                                    ,T0.U_StartPortCode,T0.U_StartPortThCode,T0.U_StartPort,T0.U_StartPortTwCode,T0.U_StartPortENamel,T0.U_TrsferPortCode,T0.U_TrsferPortThCode,T0.U_TrsferPort,T0.U_TrsferPortTwCode,T0.U_TrsferPortENamel
									,T2.U_FreightPayMethod,T2.U_senderCode,T2.U_Sender,T2.U_ReceiverCode,T2.U_Receiver
                                FROM [@AVA_FF_OBNO] T0
	                                INNER JOIN [@AVA_FF_BNO1] T1 ON T0.DocEntry = T1.DocEntry
	                                INNER JOIN [@AVA_FF_BNO4] T2 ON T0.DocEntry = T2.DocEntry
                                Where T0.DocEntry = {2}
                            UPDATE ONNM SET AutoKey = @Autokey + 1 Where ObjectCode = 'AVA_FF_Storage' 
                            ";
                        SQL = string.Format(SQL, SBOCompany.UserSignature, SBOCompany.UserName, DocEntry, SerialNum
                                , CurrentForm.DataSources.DBDataSources.Item(AVA_OBNO).GetValue("U_CardCode", 0).Trim()
                                , CurrentForm.DataSources.DBDataSources.Item(AVA_OBNO).GetValue("U_CardName", 0).Trim()
                                , CurrentForm.DataSources.DBDataSources.Item(AVA_OBNO).GetValue("U_CntctCode", 0).Trim()
                                , CurrentForm.DataSources.DBDataSources.Item(AVA_OBNO).GetValue("U_MBLNumber", 0).Trim()
                                , CurrentForm.DataSources.DBDataSources.Item(AVA_OBNO).GetValue("U_SBLNumber", 0).Trim()
                                , CurrentForm.DataSources.DBDataSources.Item(AVA_OBNO).GetValue("U_TransportType", 0).Trim()
                                , CurrentForm.DataSources.DBDataSources.Item(AVA_OBNO).GetValue("U_ReleaseAfterPay", 0).Trim()
                                , CurrentForm.DataSources.DBDataSources.Item(AVA_OBNO).GetValue("U_Customs", 0).Trim()
                                , CurrentForm.DataSources.DBDataSources.Item(AVA_OBNO).GetValue("U_ClearingForm", 0).Trim());
                        rs.DoQuery(SQL);
                    }
                }
                else
                {
                    if (IsOrderExists)
                    {
                        //删除单据
                        SQL = @"
                            DELETE T1
                            FROM [@AVA_FF_OBNO] T0
	                            INNER JOIN [@AVA_FF_OSTG] T1 ON T0.DocEntry = T1.U_BaseEntry
                            Where T0.DocEntry = '{0}'
                            ";
                        rs.DoQuery(string.Format(SQL, DocEntry));
                    }
                }
                if (SBOCompany.InTransaction) SBOCompany.EndTransaction(BoWfTransOpt.wf_Commit);
            }
            catch (Exception ex)
            {
                BubbleEvent = false;
                if (SBOCompany.InTransaction) SBOCompany.EndTransaction(BoWfTransOpt.wf_RollBack);
                throw (ex);
            }
        }
        #endregion
        #region 更新SEDL字段
        private void UpdateSEDLData()
        {
            String SBLNumber = CurrentForm.DataSources.DBDataSources.Item(AVA_OBNO).GetValue("U_SBLNumber", 0).Trim();
            if (string.IsNullOrEmpty(SBLNumber))
            {
                return;
            }
            String SQL_UpdSEDL = @"UPDATE T1 SET 
T1.U_MBLNumber=T2.U_MBLNumber, 
T1.U_Flight=T2.U_Flight, 
T1.U_StartPort=UPPER(T2.U_StartPort),
T1.U_StartPortENamel=UPPER(T5.U_CountryNameE),
T1.U_Quantity=T2.U_Quantity,
T1.U_GrossWeight=T2.U_GrossWeight,
T1.U_BillingWeight=T2.U_BillingWeight, 
T1.U_ETD=T2.U_ETD,
T1.U_ATD=T2.U_ATD,
T1.U_ETA=T2.U_ETA,
T1.U_ATA=T2.U_ATA,
T1.U_Sender=T4.U_Sender,
T1.U_Forwarder=CASE T2.U_DelegateCardName WHEN N'MUELLER+PARTNER GMBH' THEN N'M+P' ELSE T2.U_DelegateCardName END,
--T1.U_TransMode=T2.U_TransportType,
T1.U_Incoterm=T3.U_BargainMethod,
T1.U_CargoType=T3.U_CargoType,
T1.U_EndPortThCode=T2.U_EndPortThCode,
T1.U_ReleaseDate=T2.U_ClearedDate, --放行日期
T1.U_ClearedTime=T2.U_ClearedTime,
T1.U_DeliveryDate=T2.U_DeliveryDate,
T1.U_DeliveryTime=T2.U_DeliveryTime,
T1.U_RCDate=T2.U_RCDate,
T1.U_RCTime=T2.U_RCTime

FROM [@AVA_SEDL] T1
INNER JOIN [@AVA_FF_OBNO] T2 ON T1.U_SBLNumber=ISNULL(T2.U_SBLNumber,N'')
LEFT JOIN [@AVA_FF_BNO1] T3 ON T2.DocEntry=T3.DocEntry
LEFT JOIN [@AVA_FF_BNO4] T4 ON T2.DocEntry=T4.DocEntry
LEFT JOIN [@AVA_FF_PORT] T5 ON ISNULL(T2.U_StartPortCode,N'')=T5.Code 
WHERE T1.U_SBLNumber=N'{0}' ";
            Recordset rs = this.SBOCompany.GetBusinessObject(BoObjectTypes.BoRecordset);
            try
            {
                rs.DoQuery(string.Format(SQL_UpdSEDL, SBLNumber));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion
        #region 更新进口报关单的滞报日期
        private void Update_OCTCClearanceDate()
        {
            String DocEntry_OBNO = CurrentForm.DataSources.DBDataSources.Item(AVA_OBNO).GetValue("DocEntry", 0).Trim();
            if (string.IsNullOrEmpty(DocEntry_OBNO))
            {
                return;
            }
            String SQL_UpdOCTCClearanceDate = @"
UPDATE T1 SET T1.U_ClearanceDate=DATEADD(DAY,14,T2.U_ATA) FROM [@AVA_FF_OCTC] T1 
INNER JOIN [@AVA_FF_OBNO] T2 ON ISNULL(T1.U_BaseEntry,-1)=T2.DocEntry 
AND ISNULL(T1.U_TransportType,N'N')=ISNULL(T2.U_TransportType,N'')
WHERE T2.DocEntry={0} ";
            Recordset rs = this.SBOCompany.GetBusinessObject(BoObjectTypes.BoRecordset);
            try
            {
                rs.DoQuery(string.Format(SQL_UpdOCTCClearanceDate, DocEntry_OBNO));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #endregion
    }
}
